﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;

namespace TestProject
{
    public partial class NewChromosome : Form
    {
        private ChromosomeTable chrom_t;
        private List<ChromosomeGeneLink> chrom_gene_links = new List<ChromosomeGeneLink>();
        private List<CrossingoverLink> cross_links = new List<CrossingoverLink>();
        public NewChromosome(ChromosomeTable ch, List<ChromosomeGeneLink> list, List<CrossingoverLink> list1)
        {
            InitializeComponent();
            chrom_t = ch;
            chrom_gene_links = list;
            cross_links = list1;
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(chrom_t.path));
            foreach (XmlNode node in doc.GetElementsByTagName("chromosome"))
            {
                if (node.Attributes[1].Value == name_box.Text)
                {
                    MessageBox.Show("Chromosome with this name already exists.\nPlease choose another name.");
                    return;
                }
            }
            int id = Functions.set_id(chrom_t);
            MyXmlDocument doc1;
            foreach (ChromosomeGeneLink link in chrom_gene_links)
            {
                SetLink:
                    SetChromGeneLink set_link = new SetChromGeneLink(link, amount_box.Value, id);
                    if (set_link.ShowDialog(this) != DialogResult.OK)
                    {
                        MessageBox.Show("Unable to add chromosome without a link.\nPlease, create it.");
                        goto SetLink;
                    }
            }
            XmlElement el;
            foreach (CrossingoverLink link in cross_links)
            {
                doc1 = new MyXmlDocument();
                doc1.LoadXml(System.IO.File.ReadAllText(link.path));
                el = doc1.CreateElement("chromosome");
                el.SetAttribute("Id", id.ToString());
                XmlElement el1 = doc1.CreateElement("Probalities");
                for (int i = 0; i < amount_box.Value; i++)
                {
                    el1.InnerText += "100";
                    if (i < amount_box.Value - 1)
                        el1.InnerText += " ";
                }
                el.AppendChild(el1);
                doc1.DocumentElement.AppendChild(el);
                doc1.Increment(doc1.DocumentElement.Attributes[3]);
                doc1.Save(link.path);
            }
            el = doc.CreateElement("chromosome");
            el.SetAttribute("Id", id.ToString());
            el.SetAttribute("Name", name_box.Text);
            el.SetAttribute("LocusesAmount", amount_box.Value.ToString());
            doc.GetElementsByTagName("chromosomes")[0].AppendChild(el);
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.Save(chrom_t.path);
            this.Close();
        }        
        private void canc_butt_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
